<?php
/**
 * 销售物料的队列任务导入最终执行Lib类
 *
 * @author wangbiao@shopex.cn
 * @version 0.1
 */

class material_sales_material_to_import
{
    /**
     * 销售物料关联条码的队列任务执行
     * 
     * @param String $cursor_id
     * @param Array $params
     * @param String $errmsg
     * @return Boolean
     */
    function run(&$cursor_id,$params,&$errmsg)
    {
        $importObj    = app::get($params['app'])->model($params['mdl']);
        $salesBasicMaterialObj = app::get('material')->model('sales_basic_material');
        $salesMaterialExtObj = app::get('material')->model('sales_material_ext');
        $salesMaterialShopFreezeObj = app::get('material')->model('sales_material_shop_freeze');
        
        $dataSdf    = $params['sdfdata'];
        foreach ($dataSdf as $v)
        {
            //销售物料主表sales_material
            $importData = array(
                    'sales_material_name' => $v['sales_material_name'],
                    'sales_material_bn' => $v['sales_material_bn'],
                    'sales_material_bn_crc32' => $v['sales_material_bn_crc32'],
                    'shop_id' => $v['shop_id'],
                    'sales_material_type' => $v['sales_material_type'],
                    'is_bind' => $v['is_bind'],
                    'create_time' => time(),
                    'disabled' => 'false',
            );
            $is_save    = $importObj->save($importData);
            
            if($is_save)
            {
                $is_bind = false;
                
                //如果有关联物料就做绑定操作
                if(($v['sales_material_type'] == 1 || $v['sales_material_type'] == 3) && !empty($v['bind_bm_id']))
                {
                    //普通或赠品销售物料关联
                    $addBindData = array(
                                        'sm_id' => $importData['sm_id'],
                                        'bm_id' => $v['bind_bm_id'],
                                        'number' => 1,
                                    );
                    $salesBasicMaterialObj->insert($addBindData);
                    
                    $is_bind = true;
                }
                elseif(($v['sales_material_type'] == 2) && !empty($v['bind_bm_id']))
                {
                    //促销销售物料关联
                    foreach($v['bind_bm_id'] as $bm_key => $bm_val)
                    {
                        $addBindData = array(
                                            'sm_id' => $importData['sm_id'],
                                            'bm_id' => $bm_key,
                                            'number' => $bm_val['number'],
                                            'rate' => $bm_val['rate'],
                                        );
                        $salesBasicMaterialObj->insert($addBindData);
                        
                        $addBindData = null;
                    }
                    
                    $is_bind = true;
                }
                
                //如果有绑定物料数据，设定销售物料为绑定状态
                if($is_bind)
                {
                    $importObj->update(array('is_bind'=>1), array('sm_id'=>$importData['sm_id']));
                }
                
                //保存销售物料扩展信息
                $addExtData = array(
                        'sm_id' => $importData['sm_id'],
                        'cost' => $v['cost'],
                        'retail_price' => $v['retail_price'],
                        'weight' => $v['weight'],
                        'unit' => $v['unit'],
                );
                $salesMaterialExtObj->insert($addExtData);
                
                //保存销售物料店铺级冻结
                if($v['shop_id'] != '_ALL_')
                {
                    $addStockData = array(
                            'sm_id' => $importData['sm_id'],
                            'shop_id' => $v['shop_id'],
                            'shop_freeze' => 0,
                    );
                    $salesMaterialShopFreezeObj->insert($addStockData);
                }
            }else{
                $m = $importObj->db->errorinfo();
                if(!empty($m)){
                    $errmsg.=$m.";";
                }
            }
         }
         
         return false;
    }
}
